package cn.iocoder.yudao.module.system.controller.admin.expert;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.expert.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.expert.ExpertDO;
import cn.iocoder.yudao.module.system.service.expert.ExpertService;

@Tag(name = "管理后台 - 专家")
@RestController
@RequestMapping("/system/expert")
@Validated
public class ExpertController {

    @Resource
    private ExpertService expertService;

    @PostMapping("/create")
    @Operation(summary = "创建专家")
    @PreAuthorize("@ss.hasPermission('system:expert:create')")
    public CommonResult<Long> createExpert(@Valid @RequestBody ExpertSaveReqVO createReqVO) {
        return success(expertService.createExpert(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专家")
    @PreAuthorize("@ss.hasPermission('system:expert:update')")
    public CommonResult<Boolean> updateExpert(@Valid @RequestBody ExpertSaveReqVO updateReqVO) {
        expertService.updateExpert(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专家")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:expert:delete')")
    public CommonResult<Boolean> deleteExpert(@RequestParam("id") Long id) {
        expertService.deleteExpert(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专家")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:expert:query')")
    public CommonResult<ExpertRespVO> getExpert(@RequestParam("id") Long id) {
        ExpertDO expert = expertService.getExpert(id);
        return success(BeanUtils.toBean(expert, ExpertRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专家分页")
    @PreAuthorize("@ss.hasPermission('system:expert:query')")
    public CommonResult<PageResult<ExpertRespVO>> getExpertPage(@Valid ExpertPageReqVO pageReqVO) {
        PageResult<ExpertDO> pageResult = expertService.getExpertPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpertRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出专家 Excel")
    @PreAuthorize("@ss.hasPermission('system:expert:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpertExcel(@Valid ExpertPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpertDO> list = expertService.getExpertPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专家.xls", "数据", ExpertRespVO.class,
                        BeanUtils.toBean(list, ExpertRespVO.class));
    }

}